var LOC_TVSHOWSLIST = 0;
var LOC_MAINLINK = 1;
var CURRENT_LOCATION = 0;

var PAGE_SIZE = 5;

var animating = false;
var missedCall = false;

function screenEnable(enabled){
	actionsEnabled = enabled;
}

//tv shows list listener
addListener(menuBar = function(actionKey, currentLocation){
	if(currentLocation == LOC_TVSHOWSLIST){
		switch(actionKey){
			case ACTION_KEYDOWN_LEFT:
			keyDown_direction_showsList(posterListIndex, (posterListIndex-getIndexIncrement()));
			break;
			case ACTION_KEYUP_LEFT:
			keyUp_direction_showsList();
			break;
			case ACTION_KEYDOWN_RIGHT:
			keyDown_direction_showsList(posterListIndex, (posterListIndex+getIndexIncrement()));
			break;
			case ACTION_KEYUP_RIGHT:
			keyUp_direction_showsList();
			break;
			case ACTION_KEYDOWN_UP:
			keyDown_direction_showsList(posterListIndex, (posterListIndex-PAGE_SIZE));
			break;
			case ACTION_KEYUP_UP:
			keyUp_direction_showsList();
			break;
			case ACTION_KEYDOWN_DOWN:
			keyDown_direction_showsList(posterListIndex, (posterListIndex+PAGE_SIZE));
			break;
			case ACTION_KEYUP_DOWN:
			keyUp_direction_showsList();
			break;
			case ACTION_KEYUP_ENTER:
			keyEnterShowsList();
			break;
			case ACTION_KEYDOWN_DOUBLEKEY:
			keyDoubleMoviesList();
			break;
		}
	}
});

//main link listener
addListener(menuBar = function(actionKey, currentLocation){
	if(currentLocation == LOC_MAINLINK){
		switch(actionKey){
			case ACTION_KEYDOWN_DOUBLEKEY:
			keyUp_DownMainLink();
			break;
			case ACTION_KEYUP_ENTER:
			keyUp_EnterMainLink();
			break;
		}
	}
});

/****************************************/
//main link action methods
/****************************************/
function keyUp_DownMainLink(){
	CURRENT_LOCATION = LOC_TVSHOWSLIST;
	
	$('homeIcon').removeClassName('homeIconSelected');
	$('homeIcon').addClassName('homeIcon');
	
	//selectMovieRow("movieRow"+movieListIndex);
	//focusScroll(true);
}

function keyUp_EnterMainLink(){
	location.href = 'index.php';
}

/****************************************/
//tv show list action methods
/****************************************/
function keyDown_direction_showsList(fromIndex, toIndex){
	if(toIndex < 0){
		toIndex = posterListLength-1;
	
	}else if(toIndex >= posterListLength){
		toIndex = 0;
	}
	
	scrollPosterPanel(fromIndex, toIndex);
	updateScrollbar(fromIndex, toIndex);
	posterListIndex = toIndex;
}

function keyUp_direction_showsList(){
	if(!animating){
		missedCall = false;
		var posterRow = $("posterSpan"+posterListIndex);
		selectPoster('divPoster'+posterListIndex, 'poster'+posterListIndex, 
					 posterRow.readAttribute('seriesId'), posterRow.readAttribute('runtime'),
					 posterRow.readAttribute('firstAired'), posterRow.readAttribute('contentRating'),
					 posterRow.readAttribute('networkImage'), posterRow.readAttribute('genre'), 
					 posterRow.readAttribute('rating'));
	}else{
		missedCall = true;
	}
}

function keyDoubleMoviesList(){
	CURRENT_LOCATION = LOC_MAINLINK;
	
	$('homeIcon').removeClassName('homeIcon');
	$('homeIcon').addClassName('homeIconSelected');
}

function keyEnterShowsList(){
	var span = $('posterSpan'+posterListIndex);
	location.href = span.readAttribute('link');
}

function scrollPosterPanel(fromIndex, toIndex){
	animating = true;
	var an = new Animator({duration: 500,
						   onComplete: function(){
							  animating = false;
							  
							  if(missedCall){
									keyUp_direction_showsList();							
							  }
						  }}).addSubject(
	new NumericalStyleSubject(
		"posterTable",
		"margin-left",
		(((fromIndex*-1)*196)+765),
		(((toIndex*-1)*196)+765))).addSubject(
	new NumericalStyleSubject(
		"poster"+fromIndex,
		"width",
		"258",
		"190")).addSubject(
	new NumericalStyleSubject(
		"poster"+fromIndex,
		"height",
		"380",
		"280")).addSubject(
	new NumericalStyleSubject(
		"divPoster"+fromIndex,
		"width",
		"258",
		"190")).addSubject(
	new NumericalStyleSubject(
		"divPoster"+fromIndex,
		"height",
		"380",
		"280")).addSubject(
	new NumericalStyleSubject(
		"poster"+toIndex,
		"width",
		"190",
		"258")).addSubject(
	new NumericalStyleSubject(
		"poster"+toIndex,
		"height",
		"280",
		"380")).addSubject(
	new NumericalStyleSubject(
		"divPoster"+toIndex,
		"width",
		"190",
		"258")).addSubject(
	new NumericalStyleSubject(
		"divPoster"+toIndex,
		"height",
		"280",
		"380"));
	an.toggle();
	//unselectPoster1(fromIndex);
	//selectPoster1(toIndex);
}

function selectPoster1(index){
	$('poster'+index).removeClassName('tvPoster');
	$('poster'+index).addClassName('tvPosterHover');
	$('divPoster'+index).removeClassName('divTvPoster');
	$('divPoster'+index).addClassName('divTvPosterHover');	
}

function unselectPoster1(index){
	$('poster'+index).removeClassName('tvPosterHover');
	$('poster'+index).addClassName('tvPoster');
	$('divPoster'+index).removeClassName('divTvPosterHover');
	$('divPoster'+index).addClassName('divTvPoster');
}

//scrollbar
function updateScrollbar(fromIndex, toIndex){
	var fromPercent = ((fromIndex)/posterListLength)*100;
	var toPercent = ((toIndex)/posterListLength)*100;
	var an = new Animator().addSubject(
	new NumericalStyleSubject(
		"scrollNub",
		"margin-left",
		(fromPercent*16.90),
		(toPercent*16.90)));
	an.toggle();
}